package rpc_netty.LoadBalancerImpl;

import com.alibaba.nacos.api.naming.pojo.Instance;
import rpc_netty.interfaces.LoadBalancer;

import java.util.List;

//轮询
public class RoundRobinLoadBalancer implements LoadBalancer {
    private int index=0;
    @Override
    public Instance select(List<Instance> instances) {
        for (int i=0;i<instances.size();i++){
            System.out.println(instances.get(i).getPort());
        }
        if (index>=instances.size()){
            index%=instances.size();
        }
        return instances.get(index++);
    }
}
